package cn.shadow.game.chess.message;

import cn.shadow.bean.PlayerModule;
import cn.shadow.game.chess.ChessGame;
import cn.shadow.moulde.AHandler;
import cn.shadow.moulde.AModule;
import cn.shadow.moulde.Module;
import cn.shadow.proto.client.ChessClientProtoContent.*;
import cn.shadow.service.ChessGameService;
import cn.shadow.service.WorldService;
import cn.shadow.utils.BufferUtil;
import io.netty.buffer.ByteBuf;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created time:2020-08-19 10:34:53
 */
@AModule(ChessModuleMessages.MODULE_ID)
@Component
public class ChessModule extends Module {

    @Autowired
    private WorldService worldService;
    @Autowired
    private ChessGameService chessGameService;

    @AHandler(ChessModuleMessages.C2S_CHESS_CLICK)
    public void click(ByteBuf buffer, PlayerModule module) throws Exception {
        // 以下代码为自动生成，请不要随意修改
        final int posX = BufferUtil.readVarInt32(buffer);  //棋子x坐标
        final int posY = BufferUtil.readVarInt32(buffer);  //棋子Y坐标
        // 请在下面添加你的代码
    }

    @AHandler(ChessModuleMessages.C2S_CHESS_INVITE_PLAYER)
    public void invitePlayer(ByteBuf buffer, PlayerModule module) throws Exception {
        // 以下代码为自动生成，请不要随意修改
        final long inviterId = BufferUtil.readVarInt64(buffer);  //邀请玩家id
        final long inviteeId = BufferUtil.readVarInt64(buffer);  //被邀玩家id
        final int chessType = BufferUtil.readVarInt32(buffer);  //游戏类型
        String gameId = module.getChessPlayer().getGameId();
        ChessGame game;
        if (StringUtils.isEmpty(gameId)) { //没创建过游戏
            game = chessGameService.crate(chessType);
            module.getChessPlayer().setGameId(game.getGameId()); //创建一个
            game.addPlayer(module.getChessPlayer());
        } else {
            ChessGame chessGame = chessGameService.get(chessType, gameId);
            if (chessGame == null) { //游戏移除了
                game = chessGameService.crate(chessType);
                module.getChessPlayer().setGameId(game.getGameId());
                game.addPlayer(module.getChessPlayer());
            }
        }
        gameId = module.getChessPlayer().getGameId();
        PlayerModule inviter = worldService.getPlayerModule(inviterId);
        PlayerModule invitee = worldService.getPlayerModule(inviteeId);
        String inviterName = inviter.getName();
        InvitePlayer.Builder invitePlayer = InvitePlayer.newBuilder();
        invitePlayer.setGameType(chessType);
        invitePlayer.setInviteeId(inviteeId);
        invitePlayer.setInviterName(inviterName);
        invitePlayer.setGameId(gameId);
        invitee.sendMessage(ChessModuleMessages.InvitePlayerMsg(invitePlayer.build().toByteArray()));
    }


    @AHandler(ChessModuleMessages.C2S_CHESS_ACCEPT)
    public void accept(ByteBuf buffer, PlayerModule module) throws Exception {
        // 以下代码为自动生成，请不要随意修改
        final int chessType = BufferUtil.readVarInt32(buffer);  //游戏类型
        final String gameId = BufferUtil.readUTF(buffer);  //游戏id
        final long inviteeId = BufferUtil.readVarInt64(buffer);  //游戏id
        // 请在下面添加你的代码
        PlayerModule playerModule = worldService.getPlayerModule(inviteeId);
        String inviteeName = playerModule.getName();
        if (gameId.equals("0")) {
            moduleLog.info(String.format("%s拒绝了%s的邀请", inviteeName, module.getName()));
            playerModule.sendMessage(ChessModuleMessages.AcceptMsg(chessType, gameId, inviteeName, false));
        } else {
            moduleLog.info(String.format("%s同意了%s的邀请", inviteeName, module.getName()));
            ChessGame chessGame = chessGameService.get(chessType, gameId);
            chessGame.addPlayer(playerModule.getChessPlayer());
            playerModule.sendMessage(ChessModuleMessages.AcceptMsg(chessType, gameId, inviteeName, true));
        }
    }

    @AHandler(ChessModuleMessages.C2S_CHESS_START)
    public void start(ByteBuf buffer, PlayerModule module) throws Exception {
        // 以下代码为自动生成，请不要随意修改
        final int chessType = BufferUtil.readVarInt32(buffer);  //游戏类型
        final String gameId = BufferUtil.readUTF(buffer);  //游戏id
        // 请在下面添加你的代码
        ChessGame chessGame = chessGameService.get(chessType, gameId);
        InitBoard.Builder initBoard = InitBoard.newBuilder();
        initBoard.setGameId(gameId);
        initBoard.setGameType(chessType);
        module.sendMessage(ChessModuleMessages.StartMsg(initBoard.build().toByteArray()));
        chessGame.init();
    }


}
